﻿/// <reference path="D:\portal平台\C02_IT_P003_ITSM\MindrayAPP.ITSM\MindrayAPP.Web.ITSM\Scripts/jquery-1.7.2.min.js" />
///公共方法
function ControlCommon() {
    this.OtherValidate = {};
}
///初始化控件
ControlCommon.prototype.Init = function () {
    ////输入控制  如果输入的数字，整数，英文字母，英文字母和数字   中文，这些都定义事件模式进行处理  如果邮箱，电话等等其他的控制放到提交验证中去实现
    function InputControl() {
        ////输入限制控制
        $(document.body).on("keyup", "[InputType=1]", function () {
            if (this.value.length == 1) { this.value = this.value.replace(/[^1-9]/g, '') } else { this.value = this.value.replace(/\D/g, '') };
        });
        $(document.body).on("paste", "[InputType=1]", function () {
            if (this.value.length == 1) { this.value = this.value.replace(/[^1-9]/g, '') } else { this.value = this.value.replace(/\D/g, '') }
        });
        ////输入限制控制
        $(document.body).on("keyup", "[InputType=2]", function () {
            var regStrs = [
            ['^0(\\d+)$', '$1'], //禁止录入整数部分两位以上，但首位为0  
            ['[^\\d\\.]+$', ''], //禁止录入任何非数字和点  
            ['\\.(\\d?)\\.+', '.$1'], //禁止录入两个以上的点  
            ['^(\\d+\\.\\d{2}).+', '$1'] //禁止录入小数点后两位以上  
            ];
            for (var i = 0; i < regStrs.length; i++) {
                var reg = new RegExp(regStrs[i][0]);
                this.value = this.value.replace(reg, regStrs[i][1]);
            }
        });
        ////输入限制控制
        $(document.body).on("onBlur", "[InputType=2]", function () {
            var v = this.value;
            if (v === '') {
                v = '0.00';
            } else if (v === '0') {
                v = '0.00';
            } else if (v === '0.') {
                v = '0.00';
            } else if (/^0+\d+\.?\d*.*$/.test(v)) {
                v = v.replace(/^0+(\d+\.?\d*).*$/, '$1');
                v = inp.getRightPriceFormat(v).val;
            } else if (/^0\.\d$/.test(v)) {
                v = v + '0';
            } else if (!/^\d+\.\d{2}$/.test(v)) {
                if (/^\d+\.\d{2}.+/.test(v)) {
                    v = v.replace(/^(\d+\.\d{2}).*$/, '$1');
                } else if (/^\d+$/.test(v)) {
                    v = v + '.00';
                } else if (/^\d+\.$/.test(v)) {
                    v = v + '00';
                } else if (/^\d+\.\d$/.test(v)) {
                    v = v + '0';
                } else if (/^[^\d]+\d+\.?\d*$/.test(v)) {
                    v = v.replace(/^[^\d]+(\d+\.?\d*)$/, '$1');
                } else if (/\d+/.test(v)) {
                    v = v.replace(/^[^\d]*(\d+\.?\d*).*$/, '$1');
                    ty = false;
                } else if (/^0+\d+\.?\d*$/.test(v)) {
                    v = v.replace(/^0+(\d+\.?\d*)$/, '$1');
                    ty = false;
                } else {
                    v = '0.00';
                }
            }
            this.value = v;
        });
        $(document.body).on("paste", "[InputType=2]", function () {
            return;
        });
        $(document.body).on("keyup", "[InputType=3]", function () {
            this.value = this.value.replace(/[^\a-\z\A-\Z]/g, '');
        })
        $(document.body).on("paste", "[InputType=3]", function () {
            this.value = this.value.replace(/[^\a-\z\A-\Z]/g, '');
        })
        $(document.body).on("contextmenu", "[InputType=3]", function () {
            this.value = this.value.replace(/[^\a-\z\A-\Z]/g, '');
        });
        $(document.body).on("keyup", "[InputType=4]", function () {
            this.value = this.value.replace(/[^d|chun]/g, '')
        })
        $(document.body).on("keyup", "[InputType=5]", function () {
            this.value = this.value.replace(/[^\u4E00-\u9FA5]/g, '')
        });
        $(document.body).on("paste", "[InputType=5]", function () {
            this.value = this.value.replace(/[^\u4E00-\u9FA5]/g, '');
        });
        $(document.body).on("contextmenu", "[InputType=5]", function () {
            this.value = this.value.replace(/[^\u4E00-\u9FA5]/g, '');
        });

    }
    InputControl();
}
ControlCommon.prototype.OtherValidatefunc = function () {
    var IsValidate = true;
    $("[validateID]").each(function () {
        var $this = $(this)
        var re = OtherValidate[$this.attr("validateID")]["ValidateExpressions"];
        if (re.test($this.val())) {
            if ($this.attr("id").indexOf("_") >= 0) {
                ShowError(OtherValidate[$this.attr("validateID")]["Message"].replace("$this", $this.attr("title")));
            } else {
                var detailtitle = $this.parents("[datalist]").attr("title");
                var detailrowIndex = $this.parents("tr").find("td[filed=index]").text();
                ShowError("明细" + detailtitle + "第" + detailrowIndex + "行的" + OtherValidate[$this.attr("validateID")]["Message"].replace("$this", $this.attr("title")));
            }
            IsValidate = false;
            return;
        }
    });
    return IsValidate;
}
ControlCommon.prototype.DateAdd = function (interval, number, date) {
    switch (interval) {
        case "y": {
            date.setFullYear(date.getFullYear() + number);
            return date;
            break;
        }
        case "q": {
            date.setMonth(date.getMonth() + number * 3);
            return date;
            break;
        }
        case "m": {
            date.setMonth(date.getMonth() + number);
            return date;
            break;
        }
        case "w": {
            date.setDate(date.getDate() + number * 7);
            return date;
            break;
        }
        case "d": {
            date.setDate(date.getDate() + number);
            return date;
            break;
        }
        case "h": {
            date.setHours(date.getHours() + number);
            return date;
            break;
        }
        case "m": {
            date.setMinutes(date.getMinutes() + number);
            return date;
            break;
        }
        case "s": {
            date.setSeconds(date.getSeconds() + number);
            return date;
            break;
        }
        default: {
            date.setDate(date.getDate() + number);
            return date;
            break;
        }
    }
}
////获取默认值
ControlCommon.prototype.getDefaultValue = function (defaultvaluestr) {
    var defaultValue = "";
    switch (defaultvaluestr) {
        case "【当前用户名称】": defaultValue = userInfo.showUserName; break;
        case "【当前用户工号】": defaultValue = userInfo.userNo; break;
        case "【当前用户部门】": defaultValue = userInfo.deptName; break;
        case "【当前用户部门ID】": defaultValue = userInfo.deptID; break;
        case "【流程编号】": defaultValue = $("#Header_formNumber").val(); break;
        case "【当前时间yyyy-MM-dd】": defaultValue = GetTimeNow("yyyy-MM-dd"); break;
        case "【当前时间yyyy-MM-dd HH:mm:ss】": defaultValue = GetTimeNow("yyyy-MM-dd HH:mm:ss");; break;
        default: defaultValue = defaultvaluestr; break;
    }
    function GetTimeNow(format) {
        var date = new Date();
        var args = {
            "M+": date.getMonth() + 1,
            "d+": date.getDate(),
            "H+": date.getHours(),
            "m+": date.getMinutes(),
            "s+": date.getSeconds(),
            "q+": Math.floor((date.getMonth() + 3) / 3),  //quarter
            "S": date.getMilliseconds()
        };
        if (/(y+)/.test(format))
            format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var i in args) {
            var n = args[i];
            if (new RegExp("(" + i + ")").test(format))
                format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? n : ("00" + n).substr(("" + n).length));
        }
        return format;
    };
    return defaultValue;
}

ControlCommon.prototype.GetID = function () {
    return Number(Math.random().toString().substr(3, length) + Date.now()).toString(36);
}
var controlComm;
controlComm = new ControlCommon();